package com.zhinong.service.impl;

import com.zhinong.dto.ShoppingCartDTO;
import com.zhinong.entity.Order;
import com.zhinong.entity.ShoppingCart;
import com.zhinong.mapper.CartMapper;
import com.zhinong.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

    }

    @Override
    public List<Order> getCartOrders() {
        // 获取用户购物车数据，这里简化处理
        List<Order> orders = cartMapper.selectOrdersByUserId("currentUserId");
        return orders;
    }

    @Override
    public void updateQuantity(String productId, Integer change) {
        // 更新商品数量
        cartMapper.updateProductQuantity(productId, change);
    }

    @Override
    public void updateSelection(String id, Boolean selected, Boolean isStore) {
        if (isStore != null && isStore) {
            // 更新店铺选择状态
            cartMapper.updateStoreSelection(id, selected);
            // 同时更新店铺下所有商品的选择状态
            cartMapper.updateProductsSelectionByStore(id, selected);
        } else {
            // 更新商品选择状态
            cartMapper.updateProductSelection(id, selected);
        }
    }

    @Override
    public Map<String, Object> getCartTotal() {
        // 计算购物车总金额和商品数量
        Double totalAmount = cartMapper.calculateTotalAmount("currentUserId");
        Integer cartCount = cartMapper.calculateCartCount("currentUserId");

        Map<String, Object> result = new HashMap<>();
        result.put("totalAmount", totalAmount);
        result.put("cartCount", cartCount);
        return result;
    }
}
